<?php

namespace app\controller;

use app\BaseController;
use app\services\ActivityService;
use think\facade\App;
use think\Request;
use think\exception\ValidateException;
use app\validate\ActivityValidate;

class Index extends BaseController
{

    public function __construct(App $app, ActivityService $ActivityService)
    {
        parent::__construct($app);
        $this->services = $ActivityService;
    }

    // public function getHotData()
    // {
    //     //$data = json_decode( exec("scrapy crawl actHot -a url=https://www.smzdm.com/tag/tlpl4ln/post/"));
    //     $data = json_decode(exec("scrapy crawl actHot -a url=https://www.smzdm.com/tag/tlpl4ln/post/"));
    //     dd($data);
    // }


    public function handleActivityList()
    {
        $activityList = json_decode(exec("scrapy crawl activity"), true);

        foreach ($activityList as $k => &$v) {
            if ($v['post_link'] && strpos($v['post_link'], "/post") === false) {
                $v['post_link'] =  $v['post_link'] . "post" . "/";
            }
        }

        try {

            $this->services->transaction(function () use ($activityList) {
                $this->services->where(['type' => 0])->delete();
                $this->services->saveAll($activityList);
            });
        } catch (\Exception $e) {
            return $this->fail('新增失败：' . $e->getMessage());
        }
        return $this->success('新增成功');
    }

    public function getActivityList()
    {
        //dd($this->request->userInfo->uid);
        $where = $this->request->getMore([
            ['activity_name', ''],
            ['status','']
        ]);

        $list =  $this->services->getActivityList($where);
        return $this->success($list);
    }

    public function getArticleHot(Request $request)
    {
        $url = $request->param('post_link');
        $start_date = $request->param('start_date');
        $end_date = $request->param('end_date');
        $model = $request->param('model') ?  $request->param('model') : 'all';

        $start_time = strtotime($start_date);
        $end_time = strtotime($end_date);

        if ($model == 'single') {
            //单文章热度爬取
            $cmd = sprintf("scrapy crawl actHot -a url=%s -a start_time=%s -a end_time=%s", $url, $start_time, $end_time);
        } else {
            //全文章热度爬取
            $cmd = sprintf("scrapy crawl actHotAll -a url=%s -a start_time=%s -a end_time=%s", $url, $start_time, $end_time);
        }

        $data = json_decode(exec($cmd), true);

        array_multisort(array_column($data, 'hot'), SORT_DESC, $data); //SORT_DESC -- 倒序

        return $this->success($data);
    }

    //新增一条数据
    public function addOne(){
        $params = $this->request->param();
        $params['type'] = 1;
        try{
            validate(ActivityValidate::class)->scene('add')->batch(true)->check($params);
        }catch(ValidateException $e){
            return $this->fail($e->getError());
        }

        $row = $this->services->addOne($params);

        if($row){
            return $this->success('添加成功');
        }else{
            return $this->fail('添加失败');
        }
    }

    //更新一条记录
    public function updateOne(){
        $params = $this->request->param();
        //dd($params);
        try{
            validate(ActivityValidate::class)->scene('edit')->batch(true)->check($params);
        }catch(ValidateException $e){
            return $this->fail('数据不合法，修改失败',$e->getError());
        }

        $row = $this->services->updateOne($params);

        if($row){
            return $this->success('修改成功');
        }else{
            return $this->fail('修改失败');
        }
    }

    //删除一条记录
    public function deleteOne(){
        $id = $this->request->param('id');
       
        $row = $this->services->deleteOne($id);

        if($row){
            return $this->success('删除成功');
        }else{
            return $this->fail('删除失败');
        }

    }
}
